<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2021/3/22
  Time: 15:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <base href="<%=basePath%>">
    <title>Title</title>
</head>
<body>
    <form action="/stu/save" method="post">
        <input name="student" type="text" placeholder="请输入学生信息">
        <input type="submit" value="提交">
    </form>
    <hr>
    <div>
        <input type="text" id="id" placeholder="请输入ID"><br>
        <input type="text" id="name" placeholder="请输入姓名"><br>
        <input type="text" id="age" placeholder="请输入年龄"><br>
        <input type="text" id="subject" placeholder="请输入科目"><br>
        <input type="button" value="提交">
    </div>
    <script src="js/jquery-3.3.1.js"></script>
    <script type="text/javascript">
        $(function(){
            // alert("ok");
            $(":button").click(function () {
                var json = {"id":$("#id").val(),"name":$("#name").val(),
                    "age":$("#age").val(),"subject":$("#subject").val()};
                $.ajax({
                    url:"/stu/save2",
                    type:"post",
                    // dataType:"json",  //指定返回的是json格式
                    contentType:"application/json;charset=UTF-8", //指定后台接受数据是json格式
                    data:JSON.stringify(json),
                    success:function(data){
                        console.log(data);
                    }
                });
            });
        })
    </script>
</body>
</html>
